<?php 
namespace Admin\Controller;
use Think\Controller;
class GoodsController extends Controller{

  public function showdb(){

  	// $model = new  \Admin\Model\CarlistModel();
  	$model= M('Carlist');
  	$data['name']="宝马";
  	$data['nationnal']="兰州";
  	$data['price']="150000";
  	$res=$model->add($data);

  	if($res){
  		echo "添加数据成功";
  	}else{
  		echo "数据添加失败";
  	}
  }

  //查询数据
  public function goodslist(){
      $model=M('Carlist');
      // $res=$model->select('16');
     /* $res=$model->find(16);
      dump($res);*/
      /*$this->assign('res',$res);
      $this->display();*/
      $data['id']=7;
      $data['name']='福德';
      $data['price']=200000;
      $data['nationnal']='美国';
      $res=$model->save($data);
      if($res){
      	echo"修改成功";
      }else{
      	echo "修改失败";
      }
  }

//数据删除
  public function delete(){
  	$model=M('Carlist');
  	$res=$model->delete(23);
  	dump($res);
  }

  public function showsql(){
    /*G('start');
    $model =M('Carlist');
    //$res= $model->select('9');
    $res=$model->find(22);
    dump($res);
    //得到最后执行的sql 语句
    //echo $model->getlastsql();
    echo $model->_sql();
    echo "<pre>";
    G('end');

    echo G('start','end',3);
    echo "<pre>";
    echo G('start','end','m');
*/
    $str='kuchlen';
    trace($str,'str=','user');
  }

  public function pp(){
    echo '面积为：' .circle();
   
  }
  public function yzm(){
    echo '验证码为' .yzm();
  }

  public function rect(){
    load('@/f3');
    echo '四边形的面积为：'.rect();
  }

  public function test(){
    $model =D('Carlist');
    // $res= $model->find(22);
    // $re=$model->select();
    // $re=$model->delete('19,20');
    $model->find(7);
    $re=$model->delete();
    dump($re);
/*    $data['id']=7;
    $data['name']='volevo';
    $data['price']=200000;
    $data['nationnal']='中国';
    $data['time']='2017-3-28';
    $res=$model->save($data);
    if($res){
      echo "修改数据成功";
    }else{
      echo "修改失败";
    }*/
  }


  public function goodsadd(){
    $model=D('Carlist');
    if(IS_POST){
      if($data=$model->create()){
        echo "添加数据成功";
        dump($data);
        $model->add();
      }else{
         echo"数据添加失败";
         dump($model->getError());;
      }
    }
    $this->display();
  }

  //ThinkPHP辅助方法

  public function tab(){
    $model=M('Carlist');
    /*$data['name']='volevo';
    $data['id']=35;*/
   // $res=$model->where("name='volevo' or  id=35")->select();
    //$res=$model->limit('11,3')->select();
    //$res=$model->page(2,5)->select();
    //$res=$model->field('id,name,price')->select();
    //$res=$model->order('price asc')->select();
    //$res=$model->field('name ,count(*) as 数量,sum(price) as 总价')->
    //group('name')->select();
    /*$res=$model->field('id ,name,price')->where("name='奥迪'")->
    order('price desc')->select();*/
    //$res=$model->count();
    //$res=$model->order('price desc')->limit(1)->select();
    //$res=$model->avg('price');
    //$res=$model->field('count(*) as 总记录数,avg(price) as 平均价格')->select();
    /*$res=$model->field('name,count(*) as 总记录数,avg(price) as 平均价格,sum(price)
      as 总钱')->group('name')->select();*/
    //$res=$model->max('price');
    //echo "价格最高的是";
   //$res=$model->query('select * from carlist');
   $res=$model->execute("insert into carlist(id ,name,price,nationnal
    ,time) values(5,'吉利',250000,'中国','2017-3-30')");
    dump($res);
  }

  public function showtoken(){
    $model=D('Carlist');
    if(IS_POST){
      //autoCheckToken:令牌验证
      if($model->autoCheckToken($_POST)){
        echo "令牌验证成功";
      }else{
        echo "令牌验证失败";
      }
    }

    $this->display();

  }

  //验证码类 
  function verify(){

    $config=array(
   'fontSize' =>20,
   'imageW'=>150,
   'imageH'=>40,
   'length' =>4,
   'useCurve'=>false,
   'useNoise'=>false,
      );
    $vf=new \Think\Verify($config);
    $vf->entry();
  }

  public function login(){
  $model=D('User');
 if(IS_POST){
    $name=I('post.username');
    $password=I('post.password');
    if($data=$model->checklogin($name,$password)){
      $yzm=I('post.yzm');
      $vf=new \Think\Verify();
      if($vf->check($yzm)){
        echo "登陆成功，验证码正确";
      }else{
        echo "验证码错误";
      }
    }else{
      echo "登录失败";
      dump($model->getError());
    }
  }
    $this->display();
  }

}